package com.demo.jsf;

import com.demo.javabean.DBAccess;

public class RegisterAction {
	private String username;
	private String password1;
	private String password2;
	private String email;
	private String errMessage;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getErrMessage() {
		return errMessage;
	}

	public void setErrMessage(String errMessage) {
		this.errMessage = errMessage;
	}

	public String register() {
		// 检查密码是否相等
		if (!password1.equals(password2)) {
			errMessage = "两个密码不相等！";
			return "register";
		}
		
		// 检查用户名是否存在
		boolean isExist = false;
		DBAccess db = new DBAccess();
		if(db.createConn()) {
			String sql = "select * from user where username='"+username+"'";
			db.query(sql);
			if(db.next()) {
				isExist = true;
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}
		
		if (!isExist) {
			// 新增用户
			if(db.createConn()) {
				String sql = "insert into user(username,password,email) values('"+username+"','"+password1+"','"+email+"')";
				db.update(sql);
				db.closeStm();
				db.closeConn();
			}
			return "login";
		} else {
			return "register";
		}
	}

	public String getPassword1() {
		return password1;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
}
